<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* 
*/
class Article extends MY_Controller
{
	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->model('Main/ArticleModel','article');
	}


	//列表
	public function index()
	{
		$data['type_list'] = $this->article->get_article_type();

		$this->load->view('article_list', $data);
	}


	//获取数据
	public function get_list()
	{
		$page = $this->input->get('page');
		$page = $page ? $page : 1;

		$term['type_id'] = $this->input->get('type_id');
		$term['keyword'] = $this->input->get('keyword');

		$res = $this->article->get_list_data($term,$page);

		if ($res) 
		{
			$data = $res;
			$data['status'] = 200;
			$data['message'] = '获取成功！';
		}
		else
		{
			$data['status'] = 400;
			$data['message'] = '数据获取失败！';
		}

		echo json_encode($data);exit();
	}


	//添加、编辑文章页
	public function add_article()
	{
		$data['type_list'] = $this->article->get_article_type();

		$data['article_id'] = intval($this->uri->segment(3));

		if ($data['article_id'] > 0) 
		{
			$res = $this->article->get_article_row($data['article_id']);
		}

		$data['title'] = isset($res['title']) ? $res['title'] : '';
		$data['type_id'] = isset($res['type_id']) ? $res['type_id'] : '';
		$data['theme_img'] = isset($res['theme_img']) ? $res['theme_img'] : '';
		$data['content'] = isset($res['content']) ? $res['content'] : '';

		$data['image_upload_url'] = $this->image_load_url;

		$this->load->view('article_info',$data);
	}


	//富文本框上传图片
	public function upload_detailed_image()
	{
		$url = 'images/'.time().mb_substr($_FILES['file']['name'],-4,4,"UTF-8"); //本地存储路径

		if (move_uploaded_file($_FILES['file']['tmp_name'],$url)) 
		{	
			$upload_url = $this->image_load_url;	//图片服务器路径
			//构建CURLFile文件
			$cfile = new CURLFile($url,'image/jpg');
			$imgdata = array('media' => $cfile);

			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $upload_url);
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
			curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $imgdata );
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			$info = curl_exec($ch);
			curl_close($ch);

			$img_data = json_decode($info,TRUE);

			@unlink($url); //删除本地服务器图片

			$data['data'] = array('src'=>$img_data['data'][0]['url']);
			$data['code'] = '0';
			$data['msg'] = '上传成功';
		}
		else
		{
			$data['code'] = '400';
			$data['msg'] = '上传失败';
		}

		echo json_encode($data);exit();

	}


	//文章入库
	public function insert_article()
	{
		$field = array('type_id', 'title', 'theme_img', 'content');

		$arr = $this->input->post($field, TRUE);
		
		$article_id = $this->input->post('article_id');

		if (empty($article_id)) 
		{
			$arr['addtime'] = time();
			$res = $this->article->insert_article_data($arr);

			if ($res) 
			{
				$data['status'] = 200;
				$data['message'] = '添加成功！';
			}
			else
			{
				$data['status'] = 400;
				$data['message'] = '添加失败！';
			}
		}
		else
		{
			$res = $this->article->updata_article_data($arr,$article_id);

			if ($res) 
			{
				$data['status'] = 200;
				$data['message'] = '更新成功！';

			}
			else
			{
				$data['status'] = 400;
				$data['message'] = '更新失败！';
			}
			
		}

		echo json_encode($data);exit();

	}



	//删除文章
	public function delete_article()
	{
		$article_id = $this->input->get('id', true);

		$res = $this->article->remove_article($article_id);

		if ($res){
			$data['status'] = 200;
			$data['message'] = '删除成功！';
		}
		else{
			$data['status'] = 400;
			$data['message'] = '删除失败！';
		}

		echo json_encode($data);exit();
	}


	//文章类型
	public function type_list()
	{
		$data['type_list'] = $this->article->get_article_type();

		$this->load->view('article_type', $data);
	}


	//编辑文章类型
	public function add_type()
	{
		$type_name = $this->input->post('type_name',TRUE);
		$type_id = $this->input->post('type_id', TRUE);

		$res = $this->article->insert_article_type($type_name,$type_id);

		if ($res) 
		{
			$data['status'] = 200;
			$data['message'] = '操作成功！';

		}
		else
		{
			$data['status'] = 400;
			$data['message'] = '操作失败！';
		}

		echo json_encode($data);exit();
		
	}


	//删除类型
	public function delete_type()
	{

		$type_id = $this->input->get('type_id', true);

		$res = $this->article->remove_type($type_id);

		if ($res){
			$data['status'] = 200;
			$data['message'] = '删除成功！';
		}
		else{
			$data['status'] = 400;
			$data['message'] = '删除失败！';
		}

		echo json_encode($data);exit();

	}


}




?>